package com.xingchi.tornado.core.enums;

import com.xingchi.tornado.basic.BaseEnum;
import com.xingchi.tornado.anno.Dict;
import lombok.Getter;

import java.util.Arrays;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 金额枚举
 *
 * @author xingchi
 * @date 2025/10/19 14:36
 */
@Getter
@Dict(value = "MONEY")
public enum MoneyEnum implements BaseEnum {

    FEN(1, "分", 1L),
    JIAO(2, "角", 10L),
    YUAN(3, "元", 100L),
    ;
    private final Integer code;
    private final String desc;
    private final Long scale;

    MoneyEnum(Integer code, String desc, Long scale) {
        this.code = code;
        this.desc = desc;
        this.scale = scale;
    }

    private static final Map<Integer, MoneyEnum> CODE_MAPPINGS = Arrays.stream(MoneyEnum.values())
            .collect(Collectors.toMap(MoneyEnum::getCode, Function.identity()));

    public static MoneyEnum getInstance(Integer code) {
        return CODE_MAPPINGS.get(code);
    }

    public static MoneyEnum getInstanceOrDefault(Integer code, MoneyEnum defaultEnum) {
        return CODE_MAPPINGS.getOrDefault(code, defaultEnum);
    }

}
